package com.icemelon.catdog.service;

import com.icemelon.catdog.dao.UserDetailsMapper;
import com.icemelon.catdog.dao.UserMapper;
import com.icemelon.catdog.exception.MyException;
import com.icemelon.catdog.pojo.User;
import com.icemelon.catdog.pojo.UserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserDetailsMapper userDetailsMapper;

    @Override
    public UserDetails selectById(Integer userId) {
        UserDetails userDetails = userDetailsMapper.selectById(userId);
        return userDetails;
    }

    @Override
    public void updateUserDetails(UserDetails userDetails) throws MyException {
        //判断user表中是否有这个用户
        List<User> users = userMapper.selectUserById(userDetails.getId());
        if (users.size()==1){
            //有 继续判断user_details表中是否有这个用户
            List<UserDetails> userDetails2 = userDetailsMapper.selectUserDetailsById(userDetails.getId());
            if (userDetails2.size()==1){
                //有 更新
                userDetailsMapper.updateById(userDetails);
            }else {
                //没有 添加
                userDetailsMapper.insert(userDetails);
            }
        }
    }
}
